package com.aisoback.controller;

import com.aisoback.common.BaseResponse;
import com.aisoback.common.ResultUtils;
import com.aisoback.model.Request.QueryUserRequest;
import com.aisoback.model.Request.SaveRequest;
import com.aisoback.model.User;
import com.aisoback.service.UserService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
@Slf4j
//@CrossOrigin(origins = "*")
public class UserController {
    @Resource
    private UserService userService;

    /**
     * 新增用户
     *
     * @param save
     * @return
     */
    @PostMapping("/add")
    public BaseResponse<Integer> addUser(@RequestBody SaveRequest save) {
        String username = save.getUsername();
        String userProfile = save.getUserProfile();
        int Result = userService.addUser(username, userProfile);
        return ResultUtils.success(Result);
    }

    /**
     * 分页查询
     *
     * @param queryUserRequest
     * @return
     */
    @PostMapping("/page")
    public BaseResponse<Page<User>> getUserPage(@RequestBody QueryUserRequest queryUserRequest) {
        Integer pageNum = queryUserRequest.getPageNum();
        Integer pageSize = queryUserRequest.getPageSize();
        Page<User> userList = userService.getUserList(queryUserRequest, pageNum, pageSize);
        return ResultUtils.success(userList);
    }
}
